<?php
/**
 * > ブートストラップ View リソース
 *
 * LICENSE: Miracle Maple by Mirage is licensed
 * under a Creative Commons 表示-非営利-継承 2.1 日本 License.
 *
 * @category   bootstrap
 * @package    maple-story.jp
 * @version    8.0.0
 * @since      File available since Release 8.0.0
 * @copyright  Copyright 2011 Miracle Maple
 * @license    http://creativecommons.org/licenses/by-nc-sa/2.1/jp/     Creative Commons BY-NC-SA
 */
class Bootstrap_Resource_View extends Zend_Application_Resource_ResourceAbstract
{
    /**
     * @var Zend_View_Interface
     */
    protected $_view = null;

    /**
     * Defined by Zend_Application_Resource_Resource
     *
     * @return Zend_View
     */
    public function init()
    {
        $view = $this->getView();

        $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
        $viewRenderer->setView($view);
        Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
        return $view;
    }

    /**
     * Retrieve view object
     * 
     * @return Zend_View
     */
    public function getView()
    {
        $this->_options = $this->getOptions();
        if (null === $this->_view) {
            $this->_view = new Zend_View($this->_options);
        }
        
        // DOCTYPE 宣言
        if (array_key_exists('doctype', $this->_options)) {
            $this->_view->doctype($this->_options['doctype']);
        } else {
            $this->_view->doctype('XHTML1_STRICT');
        }

        // TITLE 設定
        if (array_key_exists('separator', $this->_options)) {
            $this->_view->headTitle()->setSeparator($this->_options['separator']);
        } else {
            $this->_view->headTitle()->setSeparator(' | ');
        }

        $this->_view->headTitle(Zend_Registry::get('DEFAULT_TITLE'));
        
        // META 設定
        $this->_view->headMeta()
		            ->setCharset($this->_options['encoding'])
			        ->appendHttpEquiv('Content-Style-Type','text/css')
			        ->appendHttpEquiv('Content-Script-Type','text/javascript');

        // LINK 設定
        $this->_view->headLink()
			->headLink(array('rel' => 'contents',
                             'href' => '/',
                             'title' => Zend_Registry::get('DEFAULT_TITLE')
                      ));
		if (array_key_exists('css', $this->_options)) {
			foreach ($this->_options['css'] as $css) {
	            $this->_view->headLink()->appendStylesheet($css, 'all');
			}
		}

		// SCRIPT 設定
		if (array_key_exists('script', $this->_options)) {
			foreach ($this->_options['script'] as $js) {
				if (!empty($js)) {
			        $this->_view->headScript()->appendFile($js);
				}
			}
		}
        
        return $this->_view;
    }
}
